package dynamicPlan;

public class minPathSumTest {
    public int minPathSum(int[][] grid) {
        int[][] dp = new int[grid.length][grid[0].length];
        dp[0][0]=grid[0][0];
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (i == 0 && j != 0) {
                    dp[i][j] = dp[i][j - 1] + grid[i][j];
                } else if (i != 0&&j==0) {
                    dp[i][j]=dp[i-1][j]+grid[i][j];
                }
                else if (i==0&&j==0){
                    dp[i][j]=grid[0][0];
                }
                else{
                    dp[i][j]=minNum(dp[i][j - 1],dp[i-1][j])+grid[i][j];
                }
            }
        }
        return dp[grid.length-1][grid[0].length-1];
    }
    public static int minNum(int a,int b){
        if (a<b){
            return a;
        }else{
            return b;
        }
    }

    public static void main(String[] args) {
        int[][]grid={{1,2,5},{3,2,1}};
        minPathSumTest minpathSumTest =new minPathSumTest();
        System.out.println(minpathSumTest.minPathSum(grid));
    }
}